/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cadi.games;

import com.cadi.aux.Coin;
import com.cadi.aux.Window;
import com.cadi.menu.MainMenu;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
/**
 *
 * @author Luis Eduardo Garay
 */
public class CurrencyMatch extends Window {
    
    private final JLabel instruction;
    private final JLabel useThis;
    private final JLabel usedCoins;
    private final JLabel goalImage;
    private final Coin firstCoin;
    private Coin secondCoin;
    private double goal;
    private int coinCounter;
    private final Coin[] coinsUsed;
    private int currentLevel;
    private MouseAdapter add1,add2,add5,add10,add_50,add20,add50,add100, add200,add500;
    
    public CurrencyMatch(boolean firstLevel){
        super();
        if(firstLevel){
            voiceFirst();
        }
        setCustomListeners();
        currentLevel = 1;
        instruction = new JLabel(new ImageIcon(getClass().getResource("/resources/match_instruction.png")));
        instruction.setBounds(150, 300, 200, 162);
        container.add(instruction, JLayeredPane.PALETTE_LAYER);
        goalImage = new JLabel(new ImageIcon(getClass().getResource("/resources/1peso-big.png")));
        goalImage.setBounds(80, 50,150 , 150);
        container.add(goalImage, JLayeredPane.PALETTE_LAYER);
        useThis = new JLabel(new ImageIcon(getClass().getResource("/resources/useThisCoins.png")));
        useThis.setBounds(453, 220,333 , 50);
        container.add(useThis, JLayeredPane.PALETTE_LAYER);
        usedCoins = new JLabel(new ImageIcon(getClass().getResource("/resources/coinsUsed.png")));
        usedCoins.setBounds(400, 400,298 ,50);
        container.add(usedCoins, JLayeredPane.PALETTE_LAYER);
        firstCoin = new Coin(0,"large");
        firstCoin.addMouseListener(add_50);
        firstCoin.setBounds(800, 170, 150, 150);
        container.add(firstCoin, JLayeredPane.PALETTE_LAYER);
        this.goal = 1;
        this.coinCounter = 0;
        this.coinsUsed = new Coin[10];
        this.window.setVisible(true);
        background.setIcon(new ImageIcon(getClass().getResource("/resources/fondo1.png")));
        
    }
    
    public void setLevelTwoEasy(){
        goal = 2;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter = 0;
        goalImage.setIcon(new ImageIcon(getClass().getResource("/resources/2pesos-big.png")));
        firstCoin.removeMouseListener(add_50);
        firstCoin.changeIcon(1);
        firstCoin.addMouseListener(add1);
        secondCoin = new Coin(0,"large");
        secondCoin.setBounds(1000, 170, 150, 150);
        secondCoin.addMouseListener(add_50);
        container.add(secondCoin, JLayeredPane.PALETTE_LAYER);
        window.repaint();
        voice.play();
    }
    
    public void setLevelThreeEasy(){
        goal = 5;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter = 0;
        goalImage.setIcon(new ImageIcon(getClass().getResource("/resources/5pesos-big.png")));
        firstCoin.changeIcon(2);
        firstCoin.removeMouseListener(add1);
        firstCoin.addMouseListener(add2);
        secondCoin.changeIcon(1);
        secondCoin.removeMouseListener(add_50);
        secondCoin.addMouseListener(add1);
        window.repaint();
        voice.play();
    }
    
    public void setLevelFourEasy(){
       goal = 5;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter = 0;
        goalImage.setIcon(new ImageIcon(getClass().getResource("/resources/5pesos-big.png")));
        firstCoin.changeIcon(1);
        firstCoin.removeMouseListener(add2);
        firstCoin.addMouseListener(add1);
        secondCoin.changeIcon(0);
        secondCoin.removeMouseListener(add1);
        secondCoin.addMouseListener(add_50);
        window.repaint(); 
        voice.play();
    }
    
    public void setLevelFiveEasy(){
        goal = 10;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter = 0;
        goalImage.setIcon(new ImageIcon(getClass().getResource("/resources/10pesos-big.png")));
        firstCoin.changeIcon(2);
        firstCoin.removeMouseListener(add1);
        firstCoin.addMouseListener(add2);
        secondCoin.changeIcon(1);
        secondCoin.removeMouseListener(add_50);
        secondCoin.addMouseListener(add1);
        window.repaint();
        voice.play();
    }
    
    public void setLevelOneInt(){
        voice = java.applet.Applet.newAudioClip(getClass().getResource("/resources/empate2.wav"));
        voice.play();
        instruction.setIcon(new ImageIcon(getClass().getResource("/resources/match_instruction_int.png")));
        goal = 20;
        currentLevel = 10;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        goalImage.setIcon(new ImageIcon(getClass().getResource("/resources/billete20.jpeg")));
        goalImage.setBounds(80,50,300,165);
        firstCoin.changeIcon(5);
        firstCoin.removeMouseListener(add_50);
        firstCoin.addMouseListener(add5);
        window.repaint();
        voice.play();
    }
    public void setLevelTwoInt(){
        goal = 20;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter = 0;
        firstCoin.changeIcon(10);
        firstCoin.removeMouseListener(add5);
        firstCoin.addMouseListener(add10);
        secondCoin = new Coin(2,"large");
        secondCoin.setBounds(1000, 170, 150, 150);
        secondCoin.addMouseListener(add2);
        container.add(secondCoin, JLayeredPane.PALETTE_LAYER);
        window.repaint();
        voice.play();
    }
    
    public void setLevelThreeInt(){
        goal = 20;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter = 0;
        secondCoin.changeIcon(5);
        secondCoin.removeMouseListener(add2);
        secondCoin.addMouseListener(add5);
        window.repaint();
        voice.play();
    }
    
    public void setLevelFourInt(){
        goal = 50;
        goalImage.setIcon(new ImageIcon(getClass().getResource("/resources/billete50.jpg")));
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter = 0;
        container.remove(secondCoin);
        window.repaint();
        voice.play();
    }
    
    public void setLevelFiveInt(){
        goal = 50;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter = 0;
        secondCoin = new Coin(5,"large");
        secondCoin.setBounds(1000, 170, 150, 150);
        secondCoin.addMouseListener(add5);
        container.add(secondCoin, JLayeredPane.PALETTE_LAYER);
        window.repaint();
        voice.play();
    }
    
    public void setLevelOneHard(){
        voice = java.applet.Applet.newAudioClip(getClass().getResource("/resources/empate3.wav"));
        voice.play();
//        usedCoins.setIcon(new ImageIcon(getClass().getResource("/resources/billsUsed.png")));
        instruction.setIcon(new ImageIcon(getClass().getResource("/resources/match_instruction_int.png")));
        useThis.setIcon(new ImageIcon(getClass().getResource("/resources/useThis.png")));
        goal = 50;
        currentLevel = 20;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        goalImage.setIcon(new ImageIcon(getClass().getResource("/resources/billete50.jpg")));
        goalImage.setBounds(80,50,300,165);
        firstCoin.changeIcon(20);
        firstCoin.removeMouseListener(add_50);
        firstCoin.addMouseListener(add20);
        secondCoin = new Coin(10,"large");
        secondCoin.addMouseListener(add10);
        secondCoin.setBounds(1000, 170, 150, 150);
        container.add(secondCoin, JLayeredPane.PALETTE_LAYER);
        window.repaint();
    }
    
    public void setLevelTwoHard(){
        useThis.setIcon(new ImageIcon(getClass().getResource("/resources/useThisBills.png")));
        goal = 100;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter =0;
        goalImage.setIcon(new ImageIcon(getClass().getResource("/resources/billete100.jpg")));
        firstCoin.changeIcon(50);
        firstCoin.removeMouseListener(add20);
        firstCoin.addMouseListener(add50);
        secondCoin.setVisible(false);
        window.repaint();
        voice.play();
    }
    
    public void setLevelThreeHard(){
        goal = 200;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter =0;
        goalImage.setIcon(new ImageIcon(getClass().getResource("/resources/billete200.jpg")));
        firstCoin.changeIcon(100);
        firstCoin.removeMouseListener(add50);
        firstCoin.addMouseListener(add100);
        secondCoin.changeIcon(20);
        secondCoin.removeMouseListener(add10);
        secondCoin.addMouseListener(add20);
        secondCoin.setVisible(true);
        window.repaint();
        voice.play();
    }
    
    public void setLevelFourHard(){
        goal = 200;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter =0;
        secondCoin.changeIcon(50);
        secondCoin.removeMouseListener(add20);
        secondCoin.addMouseListener(add50);
        window.repaint();
        voice.play();
    }
    
    public void setLevelFiveHard(){
        goal = 500;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter =0;
        goalImage.setIcon(new ImageIcon(getClass().getResource("/resources/billete500.jpeg")));
        firstCoin.changeIcon(200);
        firstCoin.removeMouseListener(add100);
        firstCoin.addMouseListener(add200);
        secondCoin.changeIcon(100);
        secondCoin.removeMouseListener(add50);
        secondCoin.addMouseListener(add100);
        window.repaint();
        voice.play();
    }
    
    public void setLevelSixHard(){
        goal = 1000;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter =0;
        goalImage.setIcon(new ImageIcon(getClass().getResource("/resources/billete1000.jpg")));
        window.repaint();
        voice.play();
    }
    
    public void setLevelSevenHard(){
        goal = 1000;
        for(int i = 0; i<coinCounter;i++){
            container.remove(coinsUsed[i]);
            coinsUsed[i] = null;
        }
        coinCounter =0;
        firstCoin.changeIcon(500);
        firstCoin.removeMouseListener(add200);
        firstCoin.addMouseListener(add500);
        window.repaint();
        voice.play();
    }
    
    private void setCustomListeners(){
        add_50 = new MouseAdapter() {
                     @Override
                    public void mouseClicked(MouseEvent evt){
                    updateSelection(0);
                    }
                 };
        add1 = new MouseAdapter() {
                     @Override
                    public void mouseClicked(MouseEvent evt){
                    updateSelection(1);
                    }
                 };
        add2 = new MouseAdapter() {
                     @Override
                    public void mouseClicked(MouseEvent evt){
                    updateSelection(2);
                    }
                 };
        add5 = new MouseAdapter() {
                     @Override
                    public void mouseClicked(MouseEvent evt){
                    updateSelection(5);
                    }
                 };
        add10 = new MouseAdapter() {
                     @Override
                    public void mouseClicked(MouseEvent evt){
                    updateSelection(10);
                    }
                 };
        add20 = new MouseAdapter(){
                     @Override
                    public void mouseClicked(MouseEvent evt){
                    updateSelection(20);
                    }
                 };
        add50 = new MouseAdapter(){
                     @Override
                    public void mouseClicked(MouseEvent evt){
                    updateSelection(50);
                    }
                 };
        add100 = new MouseAdapter(){
                     @Override
                    public void mouseClicked(MouseEvent evt){
                    updateSelection(100);
                    }
                 };
        add200 = new MouseAdapter(){
                     @Override
                    public void mouseClicked(MouseEvent evt){
                    updateSelection(200);
                    }
                 };
        add500 = new MouseAdapter(){
                     @Override
                    public void mouseClicked(MouseEvent evt){
                    updateSelection(500);
                    }
                 };
    }
    private void updateSelection(int selection){
        if(selection == 0){
            goal-= .50;
        }else{
            goal -= selection;
        }
        if(goal<0){
            result = java.applet.Applet.newAudioClip(getClass().getResource("/resources/mal.wav"));
            result.play();
            JOptionPane.showMessageDialog(container,"Cuidado, te pasaste","Cuidado, te pasaste",JOptionPane.INFORMATION_MESSAGE,new ImageIcon(getClass().getResource("/resources/wrong.png")));
            goal+=selection;
        }else{
            coinCounter++;
            switch(selection){
                case 0:
                    coinsUsed[coinCounter-1] = new Coin(0, "small");
                    break;
                case 1:
                    coinsUsed[coinCounter-1] = new Coin(1,"small");
                    break;
                case 2:
                    coinsUsed[coinCounter-1] = new Coin(2,"small");
                    break;
                case 5:
                    coinsUsed[coinCounter-1] = new Coin(5,"small");
                    break;
                case 10:
                    coinsUsed[coinCounter-1] = new Coin(10,"small");
                    break;
                case 20:
                    coinsUsed[coinCounter-1] = new Coin(20,"small");
                    break;
                case 50:
                    coinsUsed[coinCounter-1] = new Coin(50,"small");
                    break;
                case 100:
                    coinsUsed[coinCounter-1] = new Coin(100,"small");
                    break;
                case 200:
                    coinsUsed[coinCounter-1] = new Coin(200,"small");
                    break;
                case 500:
                    coinsUsed[coinCounter-1] = new Coin(500,"small");
                    break;
            }
            switch(coinCounter){
                case 1:
                    coinsUsed[0].setBounds(705, 400, 75, 75);
                    break;
                case 2:
                    coinsUsed[1].setBounds(785, 400, 75, 75);
                    break;
                case 3:
                     coinsUsed[2].setBounds(865, 400, 75, 75);
                     break;
                case 4: 
                    coinsUsed[3].setBounds(945, 400, 75, 75);
                    break;
                case 5:
                    coinsUsed[4].setBounds(1025, 400, 75, 75);
                    break;
                case 6: 
                    coinsUsed[5].setBounds(705, 480, 75, 75);
                    break;
                case 7:
                    coinsUsed[6].setBounds(785, 480, 75, 75);
                    break;
                case 8:
                    coinsUsed[7].setBounds(865, 480, 75, 75);
                    break;
                case 9:
                    coinsUsed[8].setBounds(945, 480, 75, 75);
                    break;
                case 10:
                    coinsUsed[9].setBounds(1025, 480, 75, 75);
                    break;
            }
            container.add(coinsUsed[coinCounter-1],JLayeredPane.PALETTE_LAYER);
            window.repaint();
            if(goal == 0){
                result = java.applet.Applet.newAudioClip(getClass().getResource("/resources/bien.wav"));
                result.play();
                JOptionPane.showMessageDialog(container, "¡Bien Hecho!", "¡Bien Hecho!",JOptionPane.INFORMATION_MESSAGE,new ImageIcon(getClass().getResource("/resources/right.png")));
                switch(currentLevel){
                    case 1:
                        currentLevel++;
                        setLevelTwoEasy();
                        break;
                    case 2:
                        currentLevel++;
                        setLevelThreeEasy();
                        break;
                    case 3:
                        currentLevel++;
                        setLevelFourEasy();
                        break;
                    case 4:
                        currentLevel++;
                        setLevelFiveEasy();
                        break;
                    case 5:
                        MainMenu main = new MainMenu();
                        window.setVisible(false);
                        break;
                    case 10:
                        currentLevel++;
                        setLevelTwoInt();
                        break;
                    case 11:
                        currentLevel++;
                        setLevelThreeInt();
                        break;
                    case 12:
                        currentLevel++;
                        setLevelFourInt();
                        break;
                    case 13:
                        currentLevel++;
                        setLevelFiveInt();
                        break;
                    case 14:
                        MainMenu main1 = new MainMenu();
                        window.setVisible(false);
                        break;
                    case 20:
                        currentLevel++;
                        setLevelTwoHard();
                        break;
                    case 21:
                        currentLevel++;
                        setLevelThreeHard();
                        break;
                    case 22:
                        currentLevel++;
                        setLevelFourHard();
                        break;
                    case 23:
                        currentLevel++;
                        setLevelFiveHard();
                        break;
                    case 24:
                        currentLevel++;
                        setLevelSixHard();
                        break;
                    case 25:
                        currentLevel++;
                        setLevelSevenHard();
                        break;
                    case 26:
                        MainMenu last = new MainMenu();
                        window.setVisible(false);
                }
            }
        }
    }

    private void voiceFirst() {
        voice = java.applet.Applet.newAudioClip(getClass().getResource("/resources/empate1.wav"));
        voice.play();
    }
}
